<?php

namespace app\modules\device\service;

use app\common\base\CommonService;
use app\modules\device\models\DeviceData;

class DeviceDataService extends CommonService
{
    function __construct()
    {
        // 对父类service(component)初始化，并指定model
        parent::init();
        $this->model = new DeviceData();
    }

    /**
     * @param string deviceId
     */
    public function getTotalData($deviceId)
    {
        // TODO: 根据时间戳判断哪个数据为最新数据

        // 根据deviceId找到相应种类的最新数据
        $info = $this->info([
            'condition' => [
                'deviceId' => $deviceId
            ]
        ]);

        if (empty($info)) {
            return parent::error('ERROR_DEVICE_NO_DATA', '该设备无数据');
        }

        return [
            'ph' => $info['ph'],
            'cod' => $info['cod'],
            'oxygen' => $info['oxygen'],
            'nh4n' => $info['nh4n'],
            'phosphorus' => $info['phosphorus'],
            'colorDe' => $info['colorDe'],
            'turbidity' => $info['turbidity'],
            'conductivity' => $info['conductivity'],
            'flow' => $info['flow'],
            'tem' => $info['tem'],
        ];
    }
}
